package com.sara.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sara.entity.Parts;
import com.sara.entity.PartsExample;
import com.sara.mapper.PartsMapper;
import com.sara.service.PartsService;
import com.sara.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Wayne
 * @date 2019/1/11
 */
@Service
public class PartsServiceImpl implements PartsService {
    private Logger logger = LoggerFactory.getLogger(PartsServiceImpl.class);
    @Autowired
    private PartsMapper partsMapper;
    @Override
    public List<Parts> findpartsByTypeId(Integer typeId) {
        PartsExample partsExample = new PartsExample();
        partsExample.createCriteria().andTypeIdEqualTo(typeId);

        return partsMapper.selectByExample(partsExample);
    }

    @Override
    public List<Parts> findPartsByOrderId(Integer id) {

        return partsMapper.findByOrderId(id);

    }

    /**
     * 根据参数去查询列表
     *
     * @param pageNum 页码
     * @param params  零件类型 ，名称 ， 库存
     * @return
     */
    @Override
    public PageInfo<Parts> findByParams(Integer pageNum, Map<String, Object> params) {

        PageHelper.startPage(pageNum, Constant.DEFAULT_PAGE_SIZE);
        List<Parts> partsList = partsMapper.selectByParam(params);
        PageInfo<Parts> pageInfo = new PageInfo<>(partsList);
        return pageInfo;
    }



    @Override
    public Parts findByPartsNo(String partsNo) {
        PartsExample partsExample = new PartsExample();
        partsExample.createCriteria().andPartsNoEqualTo(partsNo);
        List<Parts> partsList = partsMapper.selectByExample(partsExample);
        Parts parts = null;
        if(partsList.size() != 0){
            parts = partsList.get(0);
        }
        return parts;
    }

    @Override
    public void update(Parts parts) {
        Parts parts1 = partsMapper.selectByPrimaryKey(parts.getId());
        Integer inventory = parts1.getInventory() + parts.getInventory();
        parts.setInventory(inventory);
        System.out.println("inventory相加之后的数目是" + inventory);
        partsMapper.updateByPrimaryKeySelective(parts);
        logger.debug("修改配件 : {}", findById(parts.getId()));
    }

    public Parts findById(Integer id) {
        return partsMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(Integer id) {
        partsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Parts parts) {
        partsMapper.insertSelective(parts);
        logger.debug("新增配件 : {}", parts);
    }

}
